let Axios = require('../../common/js/ajax.js'); //引入Axios方法
let Compress = require('../../common/js/compressImage.js'); //引入传图方法
let WxValidate = require('../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../common/js/Mtils.min.js');
//import WxValidate from '../../common/js/WxValidate.js'
Mtils = Mtils.Mtils
//获取应用实例
const app = getApp()
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		canvasWidth: 0,
		canvasHeight: 0,
		store_id:'',
		info: {}	
	},
	onLoad: function(e) {
		var _this = this
		this.initValidate()
		var uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		this.load = this.selectComponent("#loading");
		if(!e.id){
			_this.load.loadIng()
		}
		Axios.get(app.globalData.baseUrl + 'App/Store/Store/index', {
			uid: uid,
			time: this.timesp,
			sign: this.Md5,
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					info: data
				})
				_this.load.loadIng()				
			} else {
				_this.showModal(res)
			}
		})
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '申请店铺'
		})
	},
	chooseImage: function(e) {
		var _this = this
		var uid = app.globalData.uid || ''
		var index = e.currentTarget.dataset.type
		var type = ['album', 'camera'];
		this.setData({
			compress:true
		})
		wx.chooseImage({
			sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
			sourceType: type, //可选择性开放访问相册、相机
			success: function(res) {
				wx.showLoading({
					title: '正在上传中',
				})
				Compress.compress(res.tempFilePaths[0], _this, function(res) {
					wx.uploadFile({
						url: app.globalData.baseUrl + 'App/PublicCommon/Public/UploadPicture',
						filePath: res,
						name: 'file',
						formData: {
							uid: uid,
							sign: _this.Md5,
							time: _this.timesp
						},
						success: function(res) {
							var data = JSON.parse(res.data) //坑2：与wx.request不同，wx.uploadFile返回的是[字符串]，需要自己转为JSON格式
							console.log(data)
							var info = data.info
							wx.hideLoading();
							_this.setData({
								compress:false
							})
							if (data.status == 1) {
								if (index == 0) {
									_this.setData({
										'info.id_card_front': info.save_path
									})
								} else if (index == 1) {
									_this.setData({
										'info.id_card_background': info.save_path
									})
								} else {
									_this.setData({
										'info.id_card_hold': info.save_path
									})
								}
							}else{
								wx.hideLoading();
								_this.showModal(res)
							}
						}
					})
				})
			}
		})
	},
	deleteFunc: function(e) {
		var _this = this
		var index = e.currentTarget.dataset.id
		if (index == 0) {
			var images = this.data.info.id_card_front
			_this.setData({
				'info.id_card_front': ''
			})
		} else if (index == 1) {
			var images = this.data.info.id_card_background
			_this.setData({
				'info.id_card_background': ''
			})
		} else {
			var images = this.data.info.id_card_hold
			_this.setData({
				'info.id_card_hold': ''
			})
		}
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		console.log(e)
		app.globalData.temp = e
		wx.navigateTo({
			url: '/pages/applyShop/step/step'
		})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			name: {
				required: true,
				rangelength: [2, 10]
			},
			id_card: {
				required: true,
				idcard: true,
			},
			phone: {
				required: true,
				tel: true,
			},
			// 配置false可关闭验证
			bank_card: {
				required: true,
			},
			bank_name: {
				required: true,
				rangelength: [2, 10],
			},
			id_card_front: {
				required: true,
			},
			id_card_background: {
				required: true,
			},
			id_card_hold: {
				required: true,
			}
		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			name: {
				required: '请输入姓名',
				rangelength: '请输入2~10个汉字个汉字'
			},
			phone: {
				required: '请输入11位手机号码',
				tel: '请输入正确的手机号码',
			},
			id_card: {
				required: '请输入身份证号码',
				idcard: '请输入正确的身份证号码',
			},
			bank_card: {
				required: '请输银行卡号',
			},
			bank_name: {
				required: '请输入开户名',
				rangelength: '请输入正确的开户名'
			},
			id_card_front: {
				required: '请上传身份证正面',
			},
			id_card_background: {
				required: '请上传身份证反面',
			},
			id_card_hold: {
				required: '请上传手持身份证',
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}
})
